package com.ming.elm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.ming.elm.po.User;
import com.ming.elm.service.UserService;
import com.ming.elm.service.impl.UserServiceImpl;

public class UserController {

	private UserService us = new UserServiceImpl();

	public Object getUserByIdByPass(HttpServletRequest request) throws Exception{
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		User user = us.getUserByIdByPass(userId, password);
		if(user!=null){
			HttpSession session = request.getSession();
			session.setAttribute("userId",user.getUserId());
			// 超时时间设置,此处设为60秒
			session.setMaxInactiveInterval(60);
		}
		return user;
	}
	
	public Object getUserById(HttpServletRequest request) throws Exception{

		String userId = request.getParameter("userId");
		User user = us.getUserById(userId);

		return user;
	}
	
	public Object saveUser(HttpServletRequest request) throws Exception{
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		String userName = request.getParameter("userName");
		String str = request.getParameter("userSex");
		Integer userSex = Integer.valueOf(str);

		User user = new User();
		user.setUserId(userId);
		user.setPassword(password);
		user.setUserName(userName);
		user.setUserSex(userSex);

		int result = us.saveUser(user);

		return result;
	}

	public Object updateUserImg(HttpServletRequest request) throws Exception{
		String userImg = request.getParameter("userImg");
		String userId = request.getParameter("userId");
		User user = new User();
		user.setUserImg(userImg);
		user.setUserId(userId);


		return us.updateUserImg(user);
	}

	public Object logOut(HttpServletRequest request) throws Exception{
		// 后端session需要销毁
		HttpSession session = request.getSession();
		// 销毁session
		session.invalidate();
		String str = "not log";
		return str;
	}

	public Object updatePwd(HttpServletRequest request) throws Exception{
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");

		User user = new User();
		user.setUserId(userId);
		user.setPassword(password);

		int result = us.updatePwd(user);

		return result;
	}

	public Object isSameOldPwd(HttpServletRequest request) throws Exception{
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");

		User user = new User();
		user.setUserId(userId);
		user.setPassword(password);

		return us.isSameOldPwd(user);
	}
}
